package com.jk.mapper;

import com.jk.vo.*;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

public interface DealInfoMapper {

    //根据条件进行分页查询总条数
    @Select("<script> " +
            "SELECT count(*) " +
            "FROM deal_info " +
            " <where> 1=1" +
            "<if test=\"dealInfo !=null \">"+
            " <if test=\"oneTime != null and oneTime !=''\">deal_time &gt;=#{oneTime}</if> " +
            " <if test=\"tenTime != null and tenTime !=''\">and deal_time &lt;=#{tenTime}</if> " +
            " <if test=\"dealSerianumber != null and dealSerianumber !=''\">and deal_serianumber like '%'||#{dealSerianumber}||'%'</if> " +
            " <if test=\"dealInfo != null and dealInfo !=''\">and deal_info like '%'||#{dealInfo}||'%'</if> " +
            " <if test=\"dealObject != null and dealObject !=''\">and dealdeal_object_info like '%'||#{dealObject}||'%'</if> " +
            " <if test=\"oneMoney != null and oneMoney !=''\"> and deal_money &gt;=#{oneMoney}</if> " +
            " <if test=\"tenMoney != null and tenMoney !=''\">and deal_money &lt;=#{tenMoney}</if> " +
            " <if test=\"dealStatus != null and dealStatus !=''\">and deal_status=#{dealStatus}</if> " +
            " <if test=\"dealFundflow != null and dealFundflow !=''\">and deal_fundflow=#{dealFundflow}</if> " +
            " <if test=\"recordType != null \">and record_type=#{recordType}</if> " +
            "</if>"+
            " </where> " +
            " </script> ")
    int getDealInfoCount(DealInfo dealInfo);

    //根据条件进行分页查询数据
    @Select("<script> " +
            "SELECT * " +
            "FROM deal_info " +
            " <where> 1=1" +
            "<if test=\"dealInfo !=null \">"+
            " <if test=\"dealInfo.oneTime != null and dealInfo.oneTime !=''\">deal_time &gt;=#{dealInfo.oneTime}</if> " +
            " <if test=\"dealInfo.tenTime != null and dealInfo.tenTime !=''\">and deal_time &lt;=#{dealInfo.tenTime}</if> " +
            " <if test=\"dealInfo.dealSerianumber != null and dealInfo.dealSerianumber !=''\">and deal_serianumber like '%'||#{dealInfo.dealSerianumber}||'%'</if> " +
            " <if test=\"dealInfo.dealInfo != null and dealInfo.dealInfo !=''\">and deal_info like '%'||#{dealInfo.dealInfo}||'%'</if> " +
            " <if test=\"dealInfo.dealObject != null and dealInfo.dealObject !=''\">and dealdeal_object_info like '%'||#{dealInfo.dealObject}||'%'</if> " +
            " <if test=\"dealInfo.oneMoney != null and dealInfo.oneMoney !=''\"> and deal_money &gt;=#{dealInfo.oneMoney}</if> " +
            " <if test=\"dealInfo.tenMoney != null and dealInfo.tenMoney !=''\">and deal_money &lt;=#{dealInfo.tenMoney}</if> " +
            " <if test=\"dealInfo.dealStatus != null and dealInfo.dealStatus !=''\">and deal_status=#{dealInfo.dealStatus}</if> " +
            " <if test=\"dealInfo.dealFundflow != null and dealInfo.dealFundflow !=''\">and deal_fundflow=#{dealInfo.dealFundflow}</if> " +
            " <if test=\"dealInfo.recordType != null and dealInfo.recordType !=''\">and record_type=#{dealInfo.recordType}</if> " +
            "</if>"+
            "limit #{start},#{end}"+
            " </where> " +
            " </script> ")
    @Results({@Result(property = "dealTime",column = "deal_time"),
            @Result(property = "dealObject",column = "deal_object"),
            @Result(property = "dealSerianumber",column = "deal_serianumber"),
            @Result(property = "dealMoney",column = "deal_money"),
            @Result(property = "dealStatus",column = "deal_status"),
            @Result(property = "dealId",column = "deal_id") })
    List<DealInfo> getDealInfo(@Param("start") int start,@Param("end") int end,@Param("dealInfo") DealInfo dealInfo);

    //根据时间倒序查询前七条数据
    @Select("select * from deal_info where record_type=3 GROUP BY deal_time DESC LIMIT 0,7")
    @Results({@Result(property = "dealTime",column = "deal_time"),
            @Result(property = "dealObject",column = "deal_object"),
            @Result(property = "dealMoney",column = "deal_money"),
            @Result(property = "dealStatus",column = "deal_status")})
    List<DealInfo> getInfo();

    // 三表联查账户管理中的相应数据
    @Select("SELECT t2.user_number,t2.user_email,t3.real_Name,t2.registeTime,t4.bank_name " +
            "FROM user_account t1 LEFT JOIN user_login t2 ON t1.user_id=t2.user_Id " +
            "LEFT JOIN auth_user_idcard t3 ON t1.ver_id=t3.verify_Id LEFT JOIN account_bank " +
            " t4 on t1.account_id = t4.account_bank"+
            " WHERE t1.user_id=#{s}")
    List<Map> getAccountUser(String s);

    //查询该用户所拥有的银行卡
    @Select("SELECT t3.bank_name,t3.bank_id FROM user_account t1 LEFT JOIN user_login t2" +
            " ON t1.user_id=t2.user_Id LEFT JOIN account_bank t3 ON " +
            "t1.account_id=t3.account_bank WHERE t1.user_id=#{s}")
    List<Map> getBank(String s);

    //新增一条数据状态为等待付款
    @Insert("INSERT INTO deal_info(deal_id,deal_status,deal_money,deal_time,deal_object,deal_serianumber,record_type) " +
            "VALUES(#{dealId},#{dealStatus},#{dealMoney},#{dealTime},#{dealObject},#{dealSerianumber},#{recordType})")
    void addDeal(DealInfo dealInfo);

    //将交易状态修改为交易成功
    @Update("update deal_info set deal_status = #{dealType},deal_time =#{dealTime} where deal_id = #{dealId}")
    void putDeal(DealInfo dealInfo);

    //对账户金额进行相应修改
    @Update("update user_account set account_amount =account_amount+#{accountAmount} where user_id =#{accountId}")
    void putAccountMoney(UserAccount userAccount);

    //对对应银行卡金额进行相应修改
    @Update("update account_bank set bank_money =bank_money-#{bankMoney} where bank_id =#{bankId}")
    void putBankMoney(AccountBank accountBank);

    //判断支付密码是否正确
    @Select("SELECT COUNT(*) FROM user_account WHERE pay_pwd =#{payPwd}")
    int getPayPwd(UserAccount userAccount);

    //对账户金额进行减少修改
    @Update("update user_account set account_amount =account_amount-#{accountAmount} where user_id =#{accountId}")
    void putAccountMoneyInsert(UserAccount userAccount);

    //对对应银行卡金额进行增加修改
    @Update("update account_bank set bank_money =bank_money+#{bankMoney} where bank_id =#{bankId}")
    void putBankMoneyReduce(AccountBank accountBank);

    //通过邮箱获取对象id
    @Select("SELECT * FROM user_login WHERE user_number = #{userEmail}")
    @Results(@Result(property = "userId",column = "user_id"))
    UserInfo getOtherEmail(UserInfo userInfo);

    //通过手机号获取对象id
    @Select("SELECT * FROM user_login WHERE user_number = #{userNumber}")
    @Results(@Result(property = "userId",column = "user_id"))
    UserInfo getOtherNumber(UserInfo userInfo);

    //查询被转账人真实姓名
    @Select("SELECT * FROM user_login t1 LEFT JOIN user_account t2 ON t1.user_id = t2.user_id " +
            "LEFT JOIN auth_user_idcard t3 ON t2.ver_id = t3.verify_Id WHERE t1.user_id =#{userId}")
    @Results(@Result(property = "realName",column = "real_Name"))
    AuthUserIdcard getRealName(UserInfo userInfo);

    //对转账账户金额进行相应减少修改
    @Update("update user_account set account_amount =account_amount-#{accountAmount} where account_id =#{accountId}")
    void putSelfAccount(UserAccount userAccount);

    //对被转账账户金额进行相应增加修改
    @Update("update user_account set account_amount =account_amount+#{userAccount.accountAmount} where user_id =#{userInfo.userId}")
    void putOtherAccount(@Param("userInfo") UserInfo userInfo,@Param("userAccount") UserAccount userAccount);

    //在转账到银行卡页面根据银行卡号查询相对持卡人和卡名
    @Select("SELECT t.bank_name,t.bank_cardholder FROM account_bank t WHERE t.bank_number = #{bankNumber}")
    @Results({@Result(property = "bankName",column = "bank_name"),@Result(property = "bankCardholder",column = "bank_cardholder")})
    AccountBank getAccountBank(AccountBank accountBank);

    //对被转账银行卡金额进行相应增加修改
    @Update("update account_bank set bank_money =bank_money+#{accountAmount} where bank_number =#{accountBank.bankNumber}")
    void putOtherBank(@Param("accountBank") AccountBank accountBank,@Param("userAccount") UserAccount userAccount);

    //逻辑删除（修改状态放入回收站中）
    @Update("update deal_info set delete_status = 2 where deal_id =#{dealId}")
    void putDeleteStatus(DealInfo dealInfo);

    //查询当前用户账户余额
    @Select("SELECT t1.account_amount FROM user_account t1 LEFT JOIN user_login t2 ON t1.user_id = t2.user_id WHERE t2.user_id=#{s}")
    List<Map> getUserAccountMoney(String s);

    //查询当前账户的充值记录
    @Select("SELECT * FROM deal_info t1 WHERE t1.record_type = 1 AND t1.user_id =#{s}")
    @Results({@Result(property = "dealTime",column = "deal_time"),
            @Result(property = "dealObject",column = "deal_object"),
            @Result(property = "dealMoney",column = "deal_money"),
            @Result(property = "dealStatus",column = "deal_status")})
    List<DealInfo> getTopUpInfo(String s);

    //查询当前账户的提现记录
    @Select("SELECT * FROM deal_info t1 WHERE t1.record_type = 2 AND t1.user_id =#{s}")
    @Results({@Result(property = "dealTime",column = "deal_time"),
            @Result(property = "dealObject",column = "deal_object"),
            @Result(property = "dealMoney",column = "deal_money"),
            @Result(property = "dealStatus",column = "deal_status")})
    List<DealInfo> getDepositInfo(String s);

    //回收站批量删除
    @Delete("<script> " +
            "DELETE FROM deal_info WHERE deal_id IN "+
            "<foreach collection=\"list\" item=\"ids\" open=\"(\" close=\")\" separator=\",\">"+
            "#{ids}"+
            "</foreach>"+
            " </script> ")
    int deleteBatch(List<String> ids);

    @Select("select * from deal_info where record_type=#{status} GROUP BY deal_time DESC LIMIT 0,7")
    @Results({@Result(property = "dealTime",column = "deal_time"),
            @Result(property = "dealObject",column = "deal_object"),
            @Result(property = "dealMoney",column = "deal_money"),
            @Result(property = "dealStatus",column = "deal_status")})
    List<DealInfo> getTopUpInfo2(String s, @Param("status") int status);

    //回收站展示
    @Select("SELECT * FROM deal_info where delete_status = 2")
    @Results({@Result(property = "dealTime",column = "deal_time"),
            @Result(property = "dealObject",column = "deal_object"),
            @Result(property = "dealMoney",column = "deal_money"),
            @Result(property = "dealStatus",column = "deal_status")})
    List<DealInfo> getRecycle();
}
